package com.aboycode.gdtvlive;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    private String url;
    private Button zj, gg, nf2;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x0829) {
                mWebView.loadUrl(url);
            } else if (msg.what == 0x0000) {
                Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.webview);
        zj = findViewById(R.id.zj);
        gg = findViewById(R.id.gg);
        nf2 = findViewById(R.id.nf2);
        zj.setFocusable(true);
        zj.setFocusableInTouchMode(true);
        zj.requestFocus();
        zj.requestFocusFromTouch();//设置焦点
        zj.setNextFocusDownId(R.id.gg);
        gg.setNextFocusDownId(R.id.nf2);
        nf2.setNextFocusDownId(R.id.zj);
        zj.setNextFocusUpId(R.id.nf2);
        gg.setNextFocusUpId(R.id.zj);
        nf2.setNextFocusUpId(R.id.gg);
        zj.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    zj.setTextColor(Color.RED);
                    zj.setAlpha((float) 0.3);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //联网要异步，Manif..加用户权限
                            try {
                                Document doc = Jsoup.connect("http://www.iqiyi.com/a_19rrgi9ktl.html").get();
                                url = doc.select("a.info-intro-title").attr("href");
                                url = url.replace("http://www", "http://m");
                                //http://m.iqiyi.com/v_19rr0b8ysw.html
                            } catch (IOException e) {
                                handler.obtainMessage(0x0000, "解释网址失败").sendToTarget();
                                return;
                            }
                            handler.obtainMessage(0x0829, url).sendToTarget();
                        }
                    }).start();
                } else {
                    zj.setTextColor(Color.BLACK);
                    zj.setAlpha((float) 0.1);
                }
            }
        });
        gg.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    gg.setTextColor(Color.RED);
                    gg.setAlpha((float) 0.3);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //联网要异步，Manif..加用户权限
                            try {
                                Document doc = Jsoup.connect("http://www.iqiyi.com/a_19rrgjbh91.html").get();
                                url = doc.select("a.info-intro-title").attr("href");
                                url = url.replace("http://www", "http://m");
                                //http://m.iqiyi.com/v_19rr0b8ysw.html
                            } catch (IOException e) {
                                handler.obtainMessage(0x0000, "解释网址失败").sendToTarget();
                                return;
                            }
                            handler.obtainMessage(0x0829, url).sendToTarget();
                        }
                    }).start();
                } else {
                    gg.setTextColor(Color.BLACK);
                    gg.setAlpha((float) 0.1);
                }
            }
        });
        nf2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    nf2.setTextColor(Color.RED);
                    nf2.setAlpha((float) 0.3);
                    mWebView.loadUrl("http://www.gdtv.cn/tv/tvs2/");
                } else {
                    nf2.setTextColor(Color.BLACK);
                    nf2.setAlpha((float) 0.1);
                }
            }
        });
        mWebView.setWebViewClient(new InnerWebViewClient());
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl("");
    }

    @Override
    protected void onPause() {
        mWebView.loadUrl("");
        super.onPause();
    }

    @Override
    protected void onResume() {
        mWebView.loadUrl("http://qq.com");
        zj.requestFocus();
        zj.requestFocusFromTouch();//设置焦点
        super.onResume();
    }

    private class InnerWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);//加载下一个Html页面
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:(function() { " +
                    "var videos = document.getElementsByTagName('video');" +
                    " for(var i=0;i<videos.length;i++){videos[i].play();}})()");
            Toast.makeText(MainActivity.this, "视频自动播放", Toast.LENGTH_SHORT).show();
            view.scrollTo(0,170);
            super.onPageFinished(view, url);
        }
    }
}
